package my.engine.utils;

import my.engine.objects.entities.Property;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SettingsUtil {

    public static final String LOGIN = "login";
    public static final String PASSWD = "passwd";
    public static final String DEF_LANG = "defLang";
    public static final String UK_VISIBLE = "ukVisible";
    public static final String RU_VISIBLE = "ruVisible";
    public static final String EN_VISIBLE = "enVisible";

    private static Map<String, String> portalProps = new HashMap<String, String>();

    public static void setupPortalPrefs() {
        EntityManager em = EntityManagerFilter.createEntityManager();

        portalProps.clear();
        try {
            List<Property> propsInDB = em.createQuery("select p from Property p").getResultList();

            for (Property p : propsInDB) {
                portalProps.put(p.getName(), p.getValue());
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
        }
    }

    public static void checkDefaultSettings() {
        List<Property> defProps = new ArrayList<Property>();
        defProps.add(new Property(LOGIN, "Admin login", "root"));
        defProps.add(new Property(PASSWD, "Admin password", "root"));
        defProps.add(new Property(DEF_LANG, "Default Portal Language", "uk"));
        defProps.add(new Property(UK_VISIBLE, "Uk Lang visibile", "true"));
        defProps.add(new Property(RU_VISIBLE, "Ru Lang visibile", "true"));
        defProps.add(new Property(EN_VISIBLE, "En Lang visibile", "true"));

        EntityManager em = EntityManagerFilter.createEntityManager();

        try {
            List<Property> propsInDB = em.createQuery("select p from Property p").getResultList();

            List<Property> toRemove = new ArrayList<Property>();
            for (Property p : propsInDB) {
                for (Property def : defProps) {
                    if (def.getName().equals(p.getName())) {
                        toRemove.add(def);
                    }
                }
            }
            defProps.removeAll(toRemove);

            em.getTransaction().begin();
            for (Property p : defProps) {
                em.persist(p);
            }

            em.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
        }

        setupPortalPrefs();
    }

    public static Map<String, String> getPortalProps() {
        return portalProps;
    }

    public static List<Property> getPortalPropsAsObjects() {
        EntityManager em = EntityManagerFilter.createEntityManager();

        List<Property> propsInDB = null;
        try {
            propsInDB = (List<Property>) em.createQuery("select p from Property p").getResultList();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
        }
        return propsInDB;
    }

    public static void setPortalPropsAsObjects(List<Property> props) {
        EntityManager em = EntityManagerFilter.createEntityManager();

        try {
            em.getTransaction().begin();
            for (Property p : props) {
                portalProps.put(p.getName(), p.getValue());
                em.merge(p);                
            }
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
        }
    }

    public static void setPortalProps(Map<String, String> portalProps) {
        SettingsUtil.portalProps = portalProps;
    }

    public static void setPortalProperties(Map<String, String> newProps) {
        EntityManager em = EntityManagerFilter.createEntityManager();

        try {
            em.getTransaction().begin();
            for (String key : newProps.keySet()) {
                portalProps.put(key, newProps.get(key));
            }

            for (String key : portalProps.keySet()) {
                Property p = (Property) em.createQuery("select p from Property p where p.name = :name").setParameter("name", key).getSingleResult();
                p.setValue(portalProps.get(key));
            }

            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
        }
    }

    public static void setPortalProperty(String key, String value) {
        EntityManager em = EntityManagerFilter.createEntityManager();
        try {
            portalProps.put(key, value);
            for (String k : portalProps.keySet()) {
                Property p = (Property) em.createQuery("select p from Property p where p.name = :name").setParameter("name", k).getSingleResult();
                p.setValue(portalProps.get(k));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
        }
    }

    public static String getProperty(String key) {
        return portalProps.get(key); 
    }
}
